/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-17 19:40:44
 * @LastEditTime: 2022-08-10 16:11:04
 * @LastEditors: liutq
 * @Reference:
 */
package model

import (
	"encoding/json"
	"time"

	"github.com/go-redis/redis"
)

var Redis *redis.Client

type Order struct {
	OrderId string

	Expiretime time.Time
}

func InitRedis() (err error) {
	Redis = redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	_, err = Redis.Ping().Result()
	if err != nil {
		return err
	}
	return nil
}

func SetKey(key string, value string, expire time.Duration) {
	Redis.Set(key, value, expire)
}

func GetKey(key string) string {
	return Redis.Get(key).Val()
}

func AddJob(orderid string, expiretime time.Time) {
	var order Order
	order.OrderId = orderid
	order.Expiretime = expiretime

	data, _ := json.Marshal(order)

	Redis.RPush("orderlist", string(data))
}

func Consumer() []string {

	data := Redis.BLPop(time.Second*2, "orderlist")

	return data.Val()
}
